How to do it?:
Open the Rmarkdown file of this assignment (link) in Rstudio.
Right under each question, insert a code chunk
(you can use the hotkey Ctrl + Alt + I to add a code chunk)
and code the solution for the question.
Knit the rmarkdown file (hotkey:
Ctrl + Alt + K) to export an html.
Publish the html file to your Githiub Page.
Submission: Submit the link on Github of the assignment to Canvas
gganimate and gifski
then restart Rstudio. Using the Adult Census Income data,
make an animation using geom_point and
transition_states.library(gganimate)
## Warning: package 'gganimate' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.3
library(ggplot2)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'tibble' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.3
## Warning: package 'readr' was built under R version 4.3.3
## Warning: package 'purrr' was built under R version 4.3.3
## Warning: package 'dplyr' was built under R version 4.3.3
## Warning: package 'forcats' was built under R version 4.3.3
## Warning: package 'lubridate' was built under R version 4.3.3
library(knitr)
## Warning: package 'knitr' was built under R version 4.3.3
df = read_csv('adult_census.csv')
df <- df %>%
mutate(education.num = as.character(education.num),
workclass = as.character(workclass))
df %>% ggplot(aes(x = fnlwgt,
y = age, color=education.num))+
geom_point()+
transition_states(native.country)+
labs(title = 'country: {closest_state}')
Adult Census Income data, make an animation
using geom_bar and transition_states.df %>% ggplot(aes(x = age,
fill=sex))+
geom_bar(position = 'fill')+
transition_states(education.num) +
labs(title = 'education.num: {closest_state}')
library(dplyr)
library(ggplot2)
library(gganimate)
library(lubridate)
library(readr)
df2 <- read_csv("https://covid19.who.int/WHO-COVID-19-global-data.csv")
df2$Date_reported <- as.Date(df2$Date_reported)
df2 <- df2 %>%
filter(year(Date_reported) == 2021, !is.na(New_deaths), New_deaths > 0)
df2$month <- floor_date(df2$Date_reported, "month")
monthly_deaths <- df2 %>%
group_by(month, Country) %>%
summarise(total_deaths = sum(New_deaths, na.rm = TRUE), .groups = 'drop')
ranked_deaths <- monthly_deaths %>%
group_by(month) %>%
mutate(rank = rank(-total_deaths)) %>%
filter(rank <= 10)
a1 <- ggplot(ranked_deaths, aes(x = rank, y = total_deaths, fill = Country, label = Country)) +
geom_col() +
geom_text(aes(y = total_deaths, label = Country), hjust = 1.4) +
coord_flip(clip = "off", expand = FALSE) +
scale_x_reverse() +
labs(title = 'Top 10 Countries by COVID-19 Deaths: Month {closest_state}', x = '', y = 'Total Deaths', fill = 'Country') +
theme(plot.title = element_text(hjust = 0.5, size = 22),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
transition_states(month, transition_length = 2, state_length = 1) +
ease_aes("cubic-in-out")
animate(a1, nframes = 400)
# Load necessary libraries
library(dplyr)
library(ggplot2)
library(gganimate)
library(lubridate)
library(readr)
df2 <- read_csv("https://covid19.who.int/WHO-COVID-19-global-data.csv")
df2$Date_reported <- as.Date(df2$Date_reported)
df2_2022 <- df2 %>%
filter(year(Date_reported) == 2022, !is.na(New_cases), New_cases > 0)
df2_2022$month <- floor_date(df2_2022$Date_reported, "month")
d1 <- df2_2022 %>%
group_by(month, Country) %>%
summarise(total_new_cases = sum(New_cases, na.rm = TRUE), .groups = 'drop')
d2 <- d1 %>%
group_by(month) %>%
mutate(rank = rank(-total_new_cases))
d3 <- d2 %>%
filter(rank <= 5)
a1 <- d3 %>%
ggplot(aes(x = rank, y = total_new_cases, group = Country, fill = Country, label = Country)) +
geom_col() +
geom_text(aes(y = total_new_cases, label = Country), hjust = 1.4) +
coord_flip(clip = "off", expand = FALSE) +
scale_x_reverse() +
labs(title = 'Top 5 Countries by New COVID-19 Cases: Month {closest_state}',
x = '',
y = 'Total New Cases',
fill = 'Country') +
theme(plot.title = element_text(hjust = 0.5, size = 22),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
transition_states(month, transition_length = 2, state_length = 1) +
ease_aes("cubic-in-out")
animate(a1, nframes = 400)